import * as types from './mutation-types'
import axios from 'axios'
axios.defaults.baseURL = 'http://139.224.118.74:8080/query'
export const toggleSidebar = ({ commit }, config) => {
  if (config instanceof Object) {
    commit(types.TOGGLE_SIDEBAR, config)
  }
}

export const toggleDevice = ({ commit }, device) => commit(types.TOGGLE_DEVICE, device)

export const expandMenu = ({ commit }, menuItem) => {
  if (menuItem) {
    menuItem.expanded = menuItem.expanded || false
    commit(types.EXPAND_MENU, menuItem)
  }
}

export const switchEffect = ({ commit }, effectItem) => {
  if (effectItem) {
    commit(types.SWITCH_EFFECT, effectItem)
  }
}

export const personalInfoToggleEdit = ({ commit }) => commit(types.PERSONAL_INFO_TOGGLE_EDIT)

export const personalInfoEdit = ({ commit }, info) => commit(types.PERSONAL_INFO_EDIT, info)

export const update = ({ commit }, payload) => commit(types.UPDATE, payload)

export const getUser = ({ dispatch, commit }, loginName) => {
  axios.get('/user/username/' + loginName)
  .then(function (response) {
    commit(types.SET_USER, response.data)
    window.sessionStorage.setItem('personal.account.user', JSON.stringify(response.data))
    if (response.data.userInfo !== undefined) {
      commit(types.SET_USER_INFO, response.data.userInfo)
      window.sessionStorage.setItem('personal.info.userinfo', JSON.stringify(response.data.userInfo))
    } else {
      commit(types.SET_USER_INFO, {
        name: '',
        message: '',
        gender: 0,
        birthday: ''
      })
      window.sessionStorage.setItem('personal.info.userinfo', JSON.stringify({
        name: '',
        message: '',
        gender: 0,
        birthday: ''
      }))
    }
    if (response.data.articles !== undefined || response.data.articles !== []) {
      commit(types.SET_PROJECTS, response.data.articles)
      window.sessionStorage.setItem('projects', JSON.stringify(response.data.articles))
    } else {
      commit(types.SET_PROJECTS, null)
      window.sessionStorage.setItem('projects', '[]')
    }
  })
  .catch(function (error) {
    console.log(error)
  })
}

export const getProjects = ({ commit }, userId) => {
  axios.get('/article/g/user/id/' + userId)
  .then(function (response) {
    commit(types.GET_PROJECTS, response.data)
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const getProjectsById = ({ commit }, articleId) => {
  commit(types.GET_PROJECTS_BY_ID, articleId)
}
export const setUserInfo = ({ state, commit }, payload) => {
  axios.post('/user/info', payload)
  .then(function (response) {
    console.log('保存成功')
  })
  .catch(function (error) {
    console.log(error)
  })
  window.sessionStorage.setItem('personal.info.userinfo', JSON.stringify(state.personal.info))
}
export const getUserId = ({ commit }, loginName) => {
  axios.get('/user/' + loginName + '/id')
  .then(function (response) {
    commit(types.SET_USER_ID, response.data)
    axios.get('/user/' + response.data + '/info')
    .then(function (response) {
      commit(types.SET_USER_INFO, response.data)
    })
    .catch(function (error) {
      console.log(error)
    })
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const getUserInfo = ({ commit }, userId) => {
  axios.get('/user/' + userId + '/info')
  .then(function (response) {
    commit(types.SET_USER_INFO, response.data)
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const setUser = ({ commit }, playload) => {
  window.sessionStorage.setItem('personal.info.userinfo', JSON.stringify(playload.userInfo))
  axios.post('/user', playload)
  .then(function (response) {
    commit(types.SET_USER, playload)
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const register = ({ commit }, playload) => {
  axios.post('/sign/up/user', playload)
  .then(function (response) {
    console.log('注册成功')
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const addArticle = ({ commit }, playload) => {
  axios.post('/article/user/' + playload.userId, playload.article)
  .then(function (response) {
    console.log('保存或修改成功')
    commit(types.ADD_ARTICLE, response.data)
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const alterArticle = ({ state, commit }, playload) => {
  commit(types.ALTER_ARTICLE, playload)
}
export const alterArticleAndCommond = ({ state, commit }, playload) => {
  axios.post('/article/user/' + playload.userId, playload.article)
  .then(function (response) {
    console.log('保存或修改成功')
    commit(types.ALTER_ARTICLE, playload)
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const removeArticle = ({ commit }, playload) => {
  axios.delete('/article/' + playload.userId + '/' + playload.articleId)
  .then(function (response) {
    console.log('删除成功')
    commit(types.REMOVE_PROJECTS, playload.articleId)
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const isExistsArticleData = ({ commit }, playload) => {
  axios.get('/article/exists/' + playload.userId + '/' + playload.articleId)
  .then(function (response) {
    console.log('获取成功')
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const readContent = ({ commit }, playload) => {
  axios.get('/article/content/' + playload.userId + '/' + playload.articleId + '/' + playload.filename)
  .then(function (response) {
    console.log('获取成功')
  })
  .catch(function (error) {
    console.log(error)
  })
}
export const downloadfile = ({ commit }, playload) => {
  window.open('http://139.224.118.74:8080/query/article/download/' + playload.userId + '/' + playload.articleId + '/' + playload.filename, '_blank')
}
export const clearUser = ({ commit }, playload) => {
  commit(types.SET_USER, playload)
  commit(types.SET_USER_INFO, playload)
  commit(types.SET_PROJECTS, playload)
}
export const login = ({ commit }, payload) => commit(types.LOGIN, payload)

export const islogin = ({ commit }, payload) => commit(types.ISLOGIN)
